package render

import (
	"MetroBuilderGameJam/gameEngine/drawer"
	"MetroBuilderGameJam/gameEngine/geom"
	"MetroBuilderGameJam/gameEngine/utils"
	"MetroBuilderGameJam/world"
	"github.com/hajimehoshi/ebiten/v2"
	"image/color"
)

func NewTrainPlanningRenderer(d *drawer.Drawer, size geom.Size, lineWidth float64) *TrainPlanning {
	return &TrainPlanning{
		drawer:    d,
		offsetter: newSlotOffsetCalculator(lineWidth),
		size:      size,
		lineWidth: lineWidth,
	}
}

type TrainPlanning struct {
	size      geom.Size
	drawer    *drawer.Drawer
	offsetter *slotOffsetCalculator
	lineWidth float64
}

func (t *TrainPlanning) Draw(screen *ebiten.Image, item *world.TrainPlan) *utils.Figure {
	if item == nil || item.Position == nil {
		return utils.NoFigure()
	}

	var clr color.Color
	if item.ToBranch == nil {
		clr = drawer.ColorGray
	} else {
		clr = item.ToBranch.RailRoad().GetColor()
	}
	clr = drawer.ColorTransparent(clr, 0.3)

	pos := *item.Position
	if item.ToBranch != nil && item.IsPlaced {
		if item.CurrentGoal == item.ToBranch.To() && item.DirectionTo == item.ToBranch.ToStation() ||
			item.CurrentGoal != item.ToBranch.From() && item.DirectionTo == item.ToBranch.FromStation() {
			pos = pos.AsVec2().Add(t.offsetter.getToOffset(item.ToBranch)).AsPoint()
		} else {
			pos = pos.AsVec2().Add(t.offsetter.getFromOffset(item.ToBranch)).AsPoint()
		}
	}
	stern := t.drawer.FillSoftArrowRight(screen, pos, geom.NewVec2(t.size.Width, 0), t.size, item.Rotation, clr)
	vehicle := t.drawer.FillRectangle(screen, pos, t.size, item.Rotation, clr)
	_, _ = stern, vehicle
	return utils.NoFigure()
}
